using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using Musicman.Utilities;
using System.Windows.Forms;
using Musicman.Common;

namespace Musicman.WindowsService
{
    public partial class MusicmanService : ServiceBase
    {
        public MusicmanService()
        {
            InitializeComponent();

            SearchScheduler.Instance.ProgressChanged += new SearchJobProgerssEventHandler(Search_ProgressChanged);
            SearchScheduler.Instance.SearchJobCompleted += new SearchJobCompletedEventHandler(Search_SearchJobCompleted);
            SearchScheduler.Instance.Error += new ErrorEventHandler(Instance_Error);

        }

        void Instance_Error(FileFindErrorEventArgs e)
        {
            LogManager.LogException(e.Description, e.Exception);    
        }

        void Search_SearchJobCompleted(object sender, SearchJobCompletedEventArgs e)
        {
            TimeSpan sTime = new TimeSpan(e.SearchTime);
            TimeSpan pTime = new TimeSpan(e.Processtime);
            LogManager.LogInfo(String.Format("taskid: {0}, filesFound: {1} filesAdded: {2}, filesDeleted: {3}, searchTime: {4}, processtime: {5} \r\n",
                     e.UserState, e.FilesFound, e.FilesAdded, e.FilesDeleted, sTime, pTime));
             
        }

        void Search_ProgressChanged(SearchJobProgerssChangedEventArgs e)
        {
            LogManager.LogInfo(String.Format("taskid: {0}, taskpercentage: {1} \r\n",
                e.UserState, e.ProgressPercentage));
        }


        protected override void OnStart(string[] args)
        {
            SearchScheduler.Instance.StartCron();
        }

        protected override void OnStop()
        {
             SearchScheduler.Instance.StopCron();
        }
    }
}
